/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import blueprint4j.utils.BindStringInterface;

public class FieldStringRSAId extends FieldString implements BindStringInterface {
	

	public FieldStringRSAId(String p_name, int p_field_type, String p_description, Entity entity) {
		super(p_name, p_field_type, p_description,13, entity);
	}

	public Field newInstance(Entity entity) {
		return new FieldStringRSAId(getName(), getFieldType(), getDescription(), entity);
	}
	
    /**
     * Validates a South African ID Number with a Regular Expression and CDV
     * Check.
     * 
     * @param idNumber
     *            South African ID Number
     * @return <b>true</b> if the ID Number is valid; <b>false</b> otherwise. 
     * @throws RemoteException
     */
    public static boolean isValidIdNumber(String idNumber) {
    	if (idNumber==null || idNumber.length()==0){
    		return true;
    	}
        String regExpression = "([0-9][0-9])((?:[0][1-9])|(?:[1][0-2]))((?:[0-2][0-9])|(?:[3][0-1]))([0-9])([0-9]{3})([0-9])([0-9])([0-9])"; 
        Pattern pattern = Pattern.compile(regExpression);

        Matcher matcher = pattern.matcher(idNumber);

        if (matcher.matches()) {

            int tot1 = 0;
            for (int i = 0; i < idNumber.length() - 1; i += 2) {
                tot1 = tot1 + Integer.parseInt(idNumber.substring(i, i + 1));
            }

            StringBuffer field1 = new StringBuffer("");
            for (int i = 1; i < idNumber.length(); i += 2) {
                field1.append(idNumber.substring(i, i + 1));
            }

            String evenTotStr = (Long.parseLong(field1.toString()) * 2) + "";

            int tot2 = 0;
            for (int i = 0; i < evenTotStr.length(); i++) {
                tot2 = tot2 + Integer.parseInt(evenTotStr.substring(i, i + 1));
            }

            int lastD = (10 - ((tot1 + tot2) % 10)) % 10;
            int checkD = Integer.parseInt(idNumber.substring(12, 13));

            if (checkD == lastD) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
	
	 /**
     * Overwrite this to create a field type with specific validity requirements. Returns the issue with the field.
     * 
     * @return
     */
	public String isInvalid() {
		if (isValidIdNumber(get())){
			return null;
		}
		return "'"+get()+"' is not a valid South African ID Number";
	}

}
